#include "StdAfx.h"
#include "ModulatorStream.h"
#include "WavFile.h"
#include "WaveSample.h"

ModulatorStream::ModulatorStream(void)
{
	wavFile = NULL;
	chunkSize = 0;
	sampleIdx = 0;
}

ModulatorStream::~ModulatorStream(void)
{
}

void ModulatorStream::SetModulatorFileName(const char* fileName)
{
	if(wavFile!=NULL)
	{
		delete wavFile;
	}
	wavFile = WavFile::LoadFromFile(fileName);
}

void ModulatorStream::SetChunkSize(int chunkSize)
{
	this->chunkSize = chunkSize;
}

int ModulatorStream::GetSampleRate()
{
	return wavFile->rate;
}

Sample* ModulatorStream::GetAllData()
{
	return wavFile->sample;
}

Sample* ModulatorStream::GetData()
{
	Sample* sample = new Sample(chunkSize);
	register int i = 0;
	for(; i<chunkSize && sampleIdx<wavFile->sample->length; i++)
	{
		sample->data[i] = wavFile->sample->data[sampleIdx++];
	}
	if(i<chunkSize)
	{
		for(; i<chunkSize; i++)
		{
			sample->data[i] = 0;
		}
		sampleIdx = 0;
	}
	return sample;
}
